# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import matplotlib.pyplot as pyplot
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import sklearn.metrics as metrics

data = pd.read_csv('../diabetes.csv')

## 各个数据的相关性不高，并且基本全部是 int64 和 float64 ， 直接对 x_tain 数据的标准化 , y 因为是 0，1 不做
X_train = data.drop("Outcome", axis=1)
y_train = data["Outcome"]

ss_X = StandardScaler()
X_train_trans = ss_X.fit_transform(X_train)

# 拆分 20% 数据作为测试集
X_train_part, X_test, y_train_part, y_test = train_test_split(X_train_trans, y_train, train_size=0.8, random_state=0)

## turn back to DataFrame
X_train_part = pd.DataFrame(data=X_train_part, columns=X_train.columns)
X_test_part = pd.DataFrame(data=X_test, columns=X_train.columns)

from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import cross_val_score
from sklearn.svm import LinearSVC

svc = LinearSVC().fit(X_train_part,y_train_part)
y_predict = svc.predict(X_test_part)
print("LinearSVC")
print("Classification report for classifier %s:\n%s\n"
      % (svc, metrics.classification_report(y_test, y_predict)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(y_test, y_predict))

# loss = cross_val_score(svc, X_train_part, y=y_train_part, scoring="neg_log_loss", cv=5)
# print("Liner SVM")
# print 'logloss of each fold is: ', -loss
# print'cv logloss is:', -loss.mean()
def fit_grid_point_Linear(C, X_train, y_train, X_val, y_val):
    # 在训练集是那个利用SVC训练
    SVC2 = LinearSVC(C=C)
    SVC2 = SVC2.fit(X_train, y_train)

    # 在校验集上返回accuracy
    accuracy = SVC2.score(X_val, y_val)

    print("accuracy: {}".format(accuracy))
    return accuracy


# 需要调优的参数
C_s = np.logspace(-3, 3, 7)  # logspace(a,b,N)把10的a次方到10的b次方区间分成N份
# penalty_s = ['l1','l2']

accuracy_s = []
for i, oneC in enumerate(C_s):
    #    for j, penalty in enumerate(penalty_s):
    tmp = fit_grid_point_Linear(oneC, X_train_part, y_train_part, X_test_part, y_test)
    accuracy_s.append(tmp)

x_axis = np.log10(C_s)
# for j, penalty in enumerate(penalty_s):
pyplot.plot(x_axis, np.array(accuracy_s), 'b-')

pyplot.legend()
pyplot.xlabel('log(C)')
pyplot.ylabel('accuracy')
# pyplot.savefig('SVM_Otto.png')
pyplot.show()
